<?php


namespace Frieza\Modules\BasicCheck\Actions;

use Doraemon\model\UserNoticeSetting;
use Doraemon\service\BasicCheck\CheckCommonInfo;
use Doraemon\service\BasicCheck\CheckProfileInfo;
use Doraemon\tools\Log;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class SetVisibleYourself extends RowAction
{
    public $name = '设置为仅自己可见';
    const TOAST_ALERT = '你确定要将用户设置为仅自己可见？';

    public function handle($id, Request $request)
    {

        Log::error("reasonsViolation",[$request->input("reasonsViolation")]);

        $adminId = Admin::user()->id;
        $reasonsViolation =  $request->input("reasons_violation",'');
        $script =  $request->input("script",'');
        $otherReason =  $request->input("other_reason",'');

        $isErr = empty($otherReason) && $reasonsViolation == CheckCommonInfo::VIOLATION_TYPE_OTHER
            || !empty($otherReason) && $reasonsViolation != CheckCommonInfo::VIOLATION_TYPE_OTHER;

        if($isErr){
            return $this->response()->error('违规原因必须选择其他,其他原因才能填写内容！');
        }
        $data = [
            'admin_id' => $adminId,
            'check_status' => CheckCommonInfo::CHECK_STATUS_NO,
            'check_time' => time(),
            'reasons_violation' => $reasonsViolation,
            'other_reason' => $otherReason,
            'script' => $script,
        ];
        $invalidData = [
            'admin_id' => $adminId,
            'check_status' => CheckCommonInfo::CHECK_STATUS_INVALID,
            'check_time' => time(),
        ];
        $info = CheckProfileInfo::getInfo(['id' => $id]);
        $res = false;
        if (!empty($info)) {
            $res = CheckProfileInfo::updateInfo(['id' => $id], $data);
            //把该uid下的别的数据置为无效
            CheckProfileInfo::updateInfo(['uid' => $info['uid'], 'check_status' => 0, 'id[!]' => $id], $invalidData);
            //2，修改仅自己可见
            $hasRecord = UserNoticeSetting::get("*",['uid' => $info['uid']]);
            if($hasRecord){
                UserNoticeSetting::update(['is_view_self_profile'=>1],['uid' => $info['uid']])->rowCount();
            }else{
                UserNoticeSetting::insert(['uid' => $info['uid'],'is_view_self_profile'=>1])->rowCount();
            }
            //发送下行消息
            if (CheckCommonInfo::VIOLATION_TYPE_OTHER == $reasonsViolation) {
                $msg = $script;
            } else {
                $msg = CheckCommonInfo::SCENES_PROFILE[$reasonsViolation];
            }
            CheckCommonInfo::sendMsg($info['uid'], $msg);

        }
        if ($res) {
            return $this->response()->success('设置成功')->refresh();
        } else {
            return $this->response()->error('操作失败,稍后重试!');
        }


    }

    public function retrieveModel(Request $request)
    {

        Log::error("retrieveModel,reasonsViolation",[$request->input("reasonsViolation")]);

        if (!$key = $request->get('_key')) {
            return false;
        }

        Log::error("reasonsViolation",[$request->input("reasonsViolation")]);

        return $key;
    }


    public function form()
    {
        $this->select('reasons_violation', '违规原因')->options(CheckCommonInfo::VIOLATION_REASON);
        $this->text("other_reason","其他违规原因");
        $this->textarea("script","话术");
    }


}